import { defineStore } from "pinia";
import { ref } from "vue";
import { computed } from "vue";
import { useUserStore } from "./user";
import {addCartApi, deleteCart,getCartList,updateCart,updateCartAll} from '@/apis/cart'

export const useCartStore = defineStore("rabbit-cart", () => {
    const cartList=ref([])
    const userStore=useUserStore()
    const isLogin=computed(()=>{
        return !!userStore.userInfo?.token
    })
    console.log(isLogin.value,"isLogin")
    const updateNewList=async()=>{
        const res=await getCartList()
        cartList.value=res.result
    }
    const addCart=async(obj)=>{
         //cartList.value.push()
       if(isLogin.value){
        //已登录
        await addCartApi(obj.skuId,obj.count)
        updateNewList()
       }else{
        //未登录
        const item=cartList.value.find(item=>item.skuId===obj.skuId)
       if(item){
           item.count+=obj.count
       }
       else{
           cartList.value.push(obj)
       }
       }
    }
    const totalPrice=computed(()=>{
        return cartList.value.reduce((p,c)=>p+c.count*c.price,0)
    })
    const totalCount=computed(()=>{
        return cartList.value.reduce((p,c)=>p+c.count,0)
    })
    const getCart=async()=>{
        if(isLogin.value){
        const res=await getCartList()
        cartList.value=res.result
        console.log('获取成功')
        }
    }
    const delCart=async(skuId)=>{
       if(isLogin.value){
        //已登录
        await deleteCart([skuId])
        updateNewList()
       }else{
        //未登录
        cartList.value= cartList.value.filter(item=>item.skuId!==skuId)
    }
}
    const isAllSelected=computed(()=>{
        return cartList.value.every(item=>item.selected)
    })
    const changeSelected=async(skuId,selected)=>{
      if(isLogin.value){
        //已登录
        await updateCart(skuId,{selected})
        updateNewList()
      }
      else{
        const item= cartList.value.find(item=>item.skuId===skuId)
        item.selected=selected
      }
    }
    const changeAllSelected=async(selected)=>{
      if(isLogin.value){
        //已登录
        console.log('已登录')
        const ids= cartList.value.map(item=>item.skuId)
        console.log(ids)
       await updateCartAll(selected,ids)
       updateNewList()
      }
      else{
        cartList.value.forEach(element => {
            element.selected=selected
        });
      }
    }
    const ischeckedList=computed(()=>{
        return cartList.value.filter(item=>item.selected)
    })
    const checkedCount=computed(()=>{
        return ischeckedList.value.reduce((p,c)=>p+c.count,0)
    })
    const totalSum=computed(()=>{
        return ischeckedList.value.reduce((p,c)=>p+c.count*c.price,0)
    })
    const changeCount=async(skuId,count)=>{
        if(isLogin.value){
            //已登录
            await updateCart(skuId,{count})
            updateNewList()
          }
          else{
            const item= cartList.value.find(item=>item.skuId===skuId)
            item.count=count
          }
    }
    const clearCart=()=>{
        cartList.value=[]
    }
    return{
        cartList,
        addCart,
        totalPrice,
        totalCount,
        delCart,
        isAllSelected,
        changeSelected,
        changeAllSelected,
        ischeckedList,
        checkedCount,
        totalSum,
        getCart,
        changeCount,
        clearCart,
        updateNewList
    }
},{
    persist: true
})